<?php
/**
 * @name 仓库控制器
 * @Author : yu
 * @Created : 2016-11-21
 * @Copyright : MPE.Fiidee.Monda.Cn
 */
namespace admin\action;

use herosphp\core\Loader;
use herosphp\http\HttpRequest;
use herosphp\bean\Beans;
use herosphp\utils\AjaxResult;
use herosphp\utils\ArrayUtils;

class RepertoryAction extends CommonAction
{

    public function C_start()
    {
        parent::C_start();
    }


    protected $serviceBean = "repertory.repertory.service";
    protected $order = array('addtime' => -1);

    public function index(HttpRequest $request)
    {
        $repertoryService=Beans::get($this->serviceBean);

        $repertoryModel = Loader::model('repertory');

        $conditions = array();
        $conditions['is_delete']=0;
        if( $request -> getParameter('export')==1 ){
            $page =1;
            $pagesize =2147483648;
            $ids = $request -> getParameter('ids');
            if( $ids ){
               $conditions['id']['$in']=$ids;
            }
        }

        $items = $repertoryModel->getItems($conditions,'',array('addtime'=>-1));
        if ( !empty($items) ) {
            $ids = array();
            foreach ( $items as $k=>$val ) {

                $ids[] = $val['manager_id'];

                $items[$k]['type'] = $repertoryService->getTypeText($val['type']);

                $items[$k]['address']=$items[$k]['address'].$items[$k]['street'];

            }
            $model = Loader::model("adminManager");
            $mangers = $model->getItems(array('id' => array('$in' => $ids)));
            $mangers = ArrayUtils::changeArrayKey($mangers, 'id');
            foreach ( $items as $key => $value ) {
                $items[$key]['manager_name'] = $mangers[$value['manager_id']]['name'];
            }
        }


        if( $request->getParameter('export') == 1){
            $excel = array();
            foreach($items as $k => $v)
            {
                $tmp = array();
                $tmp[] = $v["name"];
                $tmp[] = $v["area"];
                $tmp[] = $v["type"];
                $tmp[] = $v["contact"];
                $tmp[] = $v["contact_mobile"];
                $tmp[] = $v["address"];
                $tmp[] = $v["addtime"];
                $tmp[] = $v["manager_name"];
                $excel[] = $tmp;
            }
            $header = array('仓库名称', '	仓库面积(㎡)', '性质', '联系人', '联系电话', '仓库地址', '添加时间','添加人');
            phpexcel("仓库列表",$header,$excel);
            exit();
        }

        $this->assign('items', $items);

        $this->setView('/repertory/index');

        $this->assign("title", "仓库列表");
        $this->assign('bread', array('商品管理', $this->getTemplateVar('title')));
        $this->assign('breadUrl', array('javascript:;','javascript:;'));
    }


    public function add(HttpRequest $request)
    {

        $this->assign("title", "添加仓库");
        $this->assign('bread', array('商品管理','仓库信息',$this->getTemplateVar('title')));
        $this->assign('breadUrl', array('javascript:;',url('admin/repertory/index'),'javascript:;'));
        $this->setView('/repertory/add');

    }

    public function edit(HttpRequest $request)
    {
        $repertoryService=Beans::get($this->serviceBean);

        $item = Loader::model('repertory')->getItem(array('id' => $request->getParameter('id')));
        $repertoryTypeList=$repertoryService->getTypeText();
        $this->assign("item", $item);
        $this->assign("repertoryTypeList", $repertoryTypeList);

        $this->assign("title", "编辑仓库");
        $this->assign('bread', array('商品管理','仓库信息',$this->getTemplateVar('title')));
        $this->assign('breadUrl', array('javascript:;',url('admin/repertory/index'),'javascript:;'));
        $this->setView('/repertory/edit');

    }


    public function do_add(HttpRequest $request)
    {

        $data = $request->getParameter('data');

        $data['prov'] = $request -> getParameter('province');
        $data['address'] = $request -> getParameter('detail');
        $address_json= $request -> getParameter('areaJson');
        $addressArr =json_decode($address_json,true);
        if($addressArr){
            $data['prov_txt']=$addressArr['prov'];
            $data['city_txt']=$addressArr['city'];
            $data['dist_txt']=$addressArr['dist'];

        }
        $bool = Loader::model('repertory')->add($data);

        $bool ? AjaxResult::ajaxSuccessResult() : AjaxResult::ajaxFailtureResult();
    }

    public function do_edit(HttpRequest $request)
    {
        $data = $request->getParameter('data');

        $data['prov'] = $request -> getParameter('province');
        $data['address'] = $request -> getParameter('detail');
        $address_json= $request -> getParameter('areaJson');
        $addressArr =json_decode($address_json,true);
        if($addressArr){
            $data['prov_txt']=$addressArr['prov'];
            $data['city_txt']=$addressArr['city'];
            $data['dist_txt']=$addressArr['dist'];

        }
        $bool = Loader::model('repertory')->update($data, $data['id']);
        $bool ? AjaxResult::ajaxSuccessResult() : AjaxResult::ajaxFailtureResult();
    }
    //获取详细信息
    public function getRepertory(HttpRequest $request){

        $id = $request->getParameter('id');
        $item = Loader::model('repertory')->getItem($id);
        echo $item ? json_encode(array('data'=>$item)): json_encode(array('data'=>''));
        die;

    }

    /**
     * 删除单条数据
     * @param HttpRequest $request
     */
    public function delete(HttpRequest $request)
    {

        $repertoryModel = Loader::model('repertory');
        $productModel = Loader::model('product');

        $id = $request->getParameter('id');
        if ($id == "") AjaxResult::ajaxResult('error', COM_ERR_MSG);


        if($productModel->getItem(array('repertory_id'=>$id,'is_delete' => 0))){

            AjaxResult::ajaxResult('001', '仓库下有商品，不能删除');
        }

        if ($repertoryModel->update(array('is_delete' => 1), $id)) {

            AjaxResult::ajaxSuccessResult();
        } else {
            AjaxResult::ajaxFailtureResult();
        }
    }
}
